package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.entity.Institution;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.service.IInstitutionService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.impl.SysRoleServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 系统用户信息Controller
 * 
 * @author ruoyi
 * @date 2021-05-22
 */
@Controller
@RequestMapping("/system/user")
public class SysUserController extends BaseController
{
    private String prefix = "system/user";

    @Autowired
    private ISysUserService  SysUserService;
    @Autowired
    private IInstitutionService institutionService;

    @Autowired
    private ISysRoleService roleService;
   // @RequiresPermissions("system:sysuser:view")
    @GetMapping()
    public String user(ModelMap mmap)
    {
        Institution institution =new Institution();
        mmap.put("institutions",institutionService.selectAllInstitution(institution));
        return prefix + "/sysuser";
    }

    /**
     * 查询系统用户信息列表
     */
    //@RequiresPermissions("system:sysuser:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysUser user)
    {
        startPage();
        List< SysUser> list =  SysUserService.selectSysUserList(user);
        return getDataTable(list);
    }

    /**
     * 导出系统用户信息列表
     */
    @RequiresPermissions("system:sysuser:export")
    @Log(title = "系统用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysUser user)
    {
        List<SysUser> list = SysUserService.selectSysUserList(user);
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        return util.exportExcel(list, "系统用户信息数据");
    }

    /**
     * 新增系统用户信息
     */
    @GetMapping("/add")
    public String add(ModelMap map)
    {
        map.put("institutions",institutionService.selectAllInstitution(new Institution()));
        map.put("roles",roleService.selectRoleList(new SysRole()));
        return prefix + "/add";
    }

    /**
     * 新增保存系统用户信息
     */
    @RequiresPermissions("system:sysuser:add")
    @Log(title = "系统用户信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysUser user)
    {
        if(user.getItId()!=null){
            user.setSrId(2l);
        }else {
            user.setItId(0);
        }
        return toAjax(SysUserService.insertSysUser(user));
    }

    /**
     * 修改系统用户信息
     */
    @GetMapping("/edit/{uId}")
    public String edit(@PathVariable("uId") Long uId, ModelMap mmap)
    {
        SysUser SysUser = SysUserService.selectSysUserById(uId);

        mmap.put("user", SysUser);
        return prefix + "/edit";
    }
    /**
     * 查看登录用户信息
     */
    @GetMapping("/profile")
    public String proile(ModelMap mmap)
    {
        mmap.put("user",  ShiroUtils.getSysUser());
        return prefix + "/profile"+"/profile";
    }

    /**
     * 修改保存系统用户信息
     */
    @RequiresPermissions("system:sysuser:edit")
    @Log(title = "系统用户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysUser user)
    {String str="";
        char[] chars = user.getTrueName().toCharArray();
        for (int i = 0; i < chars.length-1; i++) {
            str+=chars[i];
        }
        user.setTrueName(str);
        int rows = SysUserService.updateSysUser(user);
        return toAjax(rows);
    }

    /**
     * 删除系统用户信息
     */
    @RequiresPermissions("system:sysuser:remove")
    @Log(title = "系统用户信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(SysUserService.deleteSysUserByIds(ids));
    }

    @RequiresPermissions("system:user:resetPwd")
    @GetMapping("/resetPwd/{userId}")
    public String resetPwd(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        mmap.put("user",SysUserService.selectSysUserById(userId));
        return prefix + "/resetPwd";
    }

    @RequiresPermissions("system:user:resetPwd")
    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPwd")
    @ResponseBody
    public AjaxResult resetPwdSave(SysUser user)
    {
        int i = SysUserService.resetUserPwd(user);
        if (i == 1) {
            return success();
        }
        return error();
    }

    @PostMapping("/checkLoginNameUnique")
    @ResponseBody
    public String checkLoginNameUnique(String loginName)
    {
        return SysUserService.checkLoginNameUnique(loginName);
    }

    /**
     * 校验手机号码
     */
    @PostMapping("/checkPhoneUnique")
    @ResponseBody
    public String checkPhoneUnique(String uPhone)
    {
        return SysUserService.checkPhoneUnique(uPhone);
    }


    /**
     * 用户状态修改
     */
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
//    @RequiresPermissions("system:user:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SysUser user)
    {
        SysUserService.checkUserAllowed(user);
        return toAjax(SysUserService.changeStatus(user));
    }


}
